package com.jic.manager.controller.manage.conf;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.CmsModule;
import com.jic.manager.entity.vo.request.CmsModuleRequest;
import com.jic.manager.entity.vo.response.CmsModuleResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.CmsModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;



/**
 * @Author: Taozhouchuan 
 * @date: 2020-03-22 01:47:27
 * NFZD TODO 本页面需要修改package的路径；
 */
 @RestController
 @Slf4j
 @RequestMapping("/cmsModule")
 @Api(tags = "控制类-PC")
public class CmsModuleController {
	@Autowired
	private CmsModuleService service;
	
	 /**
		 * CMS_MODULE 查询列表（分页）
		 * @param request 前端请求参数
	  */
	 @PostMapping("/listPage")
	 @ApiOperation(value = "分页查询cmsModule列表")
	 @ResponseBody
	 public RestResult<PageResult<CmsModuleResponse>>  listCmsModuleRecordPagination(@RequestBody CmsModuleRequest request) throws Throwable {
		//分页处理
		PageResult<CmsModuleResponse> pageResult = new PageResult<>();
 		CmsModule cmsModule = new CmsModule();
		try {
			BeanUtils.copyProperties(request, cmsModule);
			List<CmsModuleResponse> list = service.listCmsModuleRecordPagination(request);
			long count = service.listPageCount(cmsModule);
			List<CmsModuleResponse> responseList = list.stream().map(temp -> {
			CmsModuleResponse bo = new CmsModuleResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		pageResult.setRows(responseList);
		pageResult.setTotal(count);
		} catch (Exception e) {
			log.info(">>>>>>>>>>查询列表异常!>>>>>>>>>>");
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
		return RestResult.success(pageResult);
	}

	/**
		 * CMS_MODULE 查询列表（不分页）
		 * @param  request 前端请求参数
	  */
		@PostMapping("/listAll")
	 @ApiOperation(value = "不分页查询cmsModule列表")
	 @ResponseBody
	 public RestResult<List<CmsModuleResponse>>  listCmsModuleAllRecord(@RequestBody CmsModuleRequest request) throws Throwable {
		//分页处理
		CmsModuleRequest cmsModuleRequest = new CmsModuleRequest();
 		CmsModule cmsModule = new CmsModule();
		BeanUtils.copyProperties(request, cmsModule);
		List<CmsModule> list = service.listCmsModuleAllRecord(cmsModule);
			List<CmsModuleResponse> responseList = list.stream().map(temp -> {
			CmsModuleResponse bo = new CmsModuleResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		return RestResult.success(responseList);
	}

	/**
	 * 单表查询详情信息
	 * @param request 查询详情请求参数
	 * @return CmsModuleResponse 查询详情响应参数
	 * author Taozhouchuan
	 * @date 2020-3-11 20:54:04
	 */
	 @PostMapping("/viewCmsModule")
	 @ApiOperation(value = "查询cmsModule详情")
	 @ResponseBody
	public RestResult<CmsModuleResponse> getCmsModuleDetail(@Validated  @RequestBody CmsModuleRequest request) {
		log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
		if (request!=null) {
			CmsModule cmsModule = new CmsModule();
			BeanUtils.copyProperties(request, cmsModule);
			CmsModule result = service.getCmsModule(cmsModule);
			log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
			if (result == null) {
				return RestResult.error("9999", "记录不存在");
			}
			CmsModuleResponse bo = new CmsModuleResponse();
			BeanUtils.copyProperties(result, bo);
			return RestResult.success(bo);
		}
		log.info("查询详情请求结束");
		return RestResult.success(new CmsModuleResponse());
	}
	/**
	 * 新增或修改记录
	 * @param request 新增或修改请求参数
	 * @return CmsModuleResponse 新增或修改响应参数
	 * author Taozhouchuan
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "保存或更新cmsModule")
	@PostMapping("/saveOrUpdateCmsModule")
	 @ResponseBody
	public RestResult<Long> saveOrUpdateCmsModule(@Validated @RequestBody CmsModuleRequest request) throws Throwable {
		//分页处理
		CmsModule cmsModule = new CmsModule();
		BeanUtils.copyProperties(request, cmsModule);
		long flag =   service.saveOrUpdateCmsModule(cmsModule);
		if(flag>0){
		   return RestResult.success(flag);
		}else{
		   throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增或修改异常");
		}
	}
	/**
	 * 修改记录
	 * @param request 修改请求参数
	 * @return CmsModuleResponse 修改响应参数
	 * author Taozhouchuan
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "修改记录cmsModule")
	@PostMapping("/updateCmsModule")
	@ResponseBody
	public RestResult<Long> updateCmsModule(@RequestBody CmsModuleRequest request) throws Throwable {
			//分页处理
			CmsModule cmsModule = new CmsModule();
			BeanUtils.copyProperties(request, cmsModule);
		long flag = service.updateCmsModule(cmsModule);
	  if(flag>0){
		 return RestResult.success(flag);
	  }else{
		  throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增或修改异常");
	  }
	}

	/**
	 * 新增记录
	 * @param request 新增请求参数
	 * @return CmsModuleResponse 新增响应参数
	 * author Taozhouchuan
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "新增记录cmsModule")
	@PostMapping("/addCmsModule")
	@ResponseBody
	public RestResult<Long> addCmsModule(@RequestBody CmsModuleRequest request) throws Throwable {
			//分页处理
			CmsModule cmsModule = new CmsModule();
			BeanUtils.copyProperties(request, cmsModule);
		long flag = service.addCmsModule(cmsModule);
	  if(flag>0){
		 return RestResult.success(flag);
	  }else{
		  throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增异常");
	  }
	}
	/**
	 * 删除记录
	 * @param request 删除请求参数
	 * @return CmsModuleResponse 删除响应参数
	 * author Taozhouchuan
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "删除cmsModule记录")
	@PostMapping("/deleteCmsModule")
	@ResponseBody
	public RestResult<Long> deleteCmsModule(@RequestBody CmsModuleRequest request) throws Throwable {
			//分页处理
			CmsModule cmsModule = new CmsModule();
			BeanUtils.copyProperties(request, cmsModule);
		long flag = service.deleteCmsModule(cmsModule);
	  if(flag>0){
		 return RestResult.success(flag);
	  }else{
		  throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除异常");
	  }
	}
	/**
	 * 逻辑删除记录
	 * @param request 逻辑删除请求参数
	 * @return CmsModuleResponse 逻辑删除响应参数
	 * author Taozhouchuan
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "逻辑删除cmsModule记录")
	@PostMapping("/deleteCmsModuleLogically")
	@ResponseBody
	public RestResult<Long> deleteCmsModuleLogically(@RequestBody CmsModuleRequest request) throws Throwable {
			//分页处理
			CmsModule cmsModule = new CmsModule();
			BeanUtils.copyProperties(request, cmsModule);
		long flag =  service.deleteCmsModuleLogically(cmsModule);
	  if(flag>0){
		 return RestResult.success(flag);
	  }else{
		  throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除异常");
	  }
	}
}
